package com.qiaoba.file.tencent.config;

import cn.hutool.extra.spring.SpringUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * 腾讯云 文件服务配置
 *
 * @author ailanyin
 * @date 2023-09-27 13:11:27
 */
@Configuration
@ComponentScan("com.qiaoba.file.tencent")
public class TencentOssConfig {

    @Value("${tencent.oss.region}")
    private String region;

    @Value("${tencent.oss.accessKey}")
    private String accessKey;

    @Value("${tencent.oss.secretKey}")
    private String secretKey;

    @Bean
    public COSClient cosClient() {
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        return new COSClient(cred, clientConfig);
    }

    @PreDestroy
    public void close() {
        SpringUtil.getBean(COSClient.class).shutdown();
    }
}
